<?php

/**
 * User login, register, and maybe profile page in the future.
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    modules
 * @subpackage admin
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: UserController.php 47 2009-05-28 17:56:56Z monzee $
 */
class Admin_UserController extends Zend_Controller_Action
{
    public function postDispatch()
    {
        if (!$this->getRequest()->isDispatched()) {
            return;
        }
        $flash = $this->_helper->flashMessenger->setNamespace('user');
        $this->view->messages = $flash->hasMessages() ? $flash->getMessages()
                                                      : $flash->getCurrentMessages();
        $flash->clearCurrentMessages();
    }

    public function indexAction()
    {
        $auth = Zend_Auth::getInstance();
        $loggedIn = $auth->hasIdentity();
        $user = $loggedIn ? $auth->getIdentity()
                          : array('username' => 'guest');
        $this->view->user = $user;
    }

    public function loginAction()
    {
        $req = $this->getRequest();
        $form = new Admin_Form_Login(array(
                'method' => 'post',
                'action' => $this->_helper->url('login'),
            ));

        $flash = $this->_helper->flashMessenger;
        if ($req->isPost() && $form->isValid($req->getPost())) {
            $auth = Zend_Auth::getInstance();
            $authAdapter = Admin_Model_Auth::getAdapter(
                $req->getPost('username'),
                Admin_Model_Auth::hash($req->getPost('password'))
            );
            $res = $auth->authenticate($authAdapter);

            if ($res->isValid()) {
                $flash->setNamespace()
                      ->addMessage('You are now logged in.');

                $origin = $form->getValue('origin');
                Admin_Bootstrap::log($origin);
                if (!empty($origin)) {
                    $this->_helper->redirector
                         ->setPrependBase(false)
                         ->gotoUrlAndExit($origin);
                } else {
                    // TODO: origin not set; redirect to user index
                }

            } else {
                $flash->setNamespace('user')
                      ->addMessage('Invalid credentials supplied.')
                      ->setNamespace('origin')
                      ->addMessage($form->getValue('origin'));

                $this->_helper->redirector
                     ->setPrependBase(false)
                     ->direct('login');
            }
        }

        if ($this->_hasParam('originParams')) {
            $origin = $this->_helper->url->url($this->_getParam('originParams'));
            $form->origin->setValue($origin);
        } else if ($flash->setNamespace('origin')->hasMessages()) {
            $origin = current($flash->getMessages());
            $form->origin->setValue($origin);
        }

        $this->view->form = $form;
    }

    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_helper->redirector
             ->setPrependBase(false)
             ->direct('index', 'index', 'default');
    }


    public function forgotAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        echo "That's too bad. Why don't you <a href=\""
             . $this->_helper->url('register')
             . "\">register</a> a new account?";
    }

    public function registerAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        echo "Registration is easy! Simply fill up the subscription card that "
             . "came with the user manual and mail it to the site owner. "
             . "Processing may take a few days to a month depending "
             . " on the volume of subscription requests at the moment.";
    }


}